{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.2 Counting sort"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.2-1\n",
    "\n",
    "> Using Figure 8.2 as a model, illustrate the operation of COUNTING-SORT on the array $A = \\left \\langle6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2\\right \\rangle$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](./img/8.2-1_1.png)\n",
    "\n",
    "![](./img/8.2-1_2.png)\n",
    "\n",
    "![](./img/8.2-1_3.png)\n",
    "\n",
    "![](./img/8.2-1_4.png)\n",
    "\n",
    "![](./img/8.2-1_5.png)\n",
    "\n",
    "![](./img/8.2-1_6.png)\n",
    "\n",
    "![](./img/8.2-1_7.png)\n",
    "\n",
    "![](./img/8.2-1_8.png)\n",
    "\n",
    "![](./img/8.2-1_9.png)\n",
    "\n",
    "![](./img/8.2-1_10.png)\n",
    "\n",
    "![](./img/8.2-1_11.png)\n",
    "\n",
    "![](./img/8.2-1_12.png)\n",
    "\n",
    "![](./img/8.2-1_13.png)\n",
    "\n",
    "![](./img/8.2-1_14.png)\n",
    "\n",
    "![](./img/8.2-1_15.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.2-2\n",
    "\n",
    "> Prove that COUNTING-SORT is stable."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Value with larger index choose the largest index first."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.2-3\n",
    "\n",
    "> Suppose that we were to rewrite the for loop header in line 10 of the COUNTING-SORT as\n",
    "```\n",
    "10 for j = 1 to A.length\n",
    "```\n",
    "Show that the algorithm still works properly. Is the modified algorithm stable?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Works properly but not stable."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.2-4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Describe an algorithm that, given n integers in the range $0$ to $k$, preprocesses its input and then answers any query about how many of the $n$ integers fall into a range $[a \\dots b]$ in $O(1)$ time. Your algorithm should use $\\Theta(n + k)$ preprocessing time.\n",
    "\n",
    "Use `C` in the counting sort, the number of integers fall into a range $[a \\dots b]$ is $C[b] - C[a-1]$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class CountInterval:\n",
    "    def __init__(self, a):\n",
    "        k = max(a)\n",
    "        self.c = [0 for _ in range(k + 1)]\n",
    "        for v in a:\n",
    "            self.c[v] += 1\n",
    "        for i in range(1, k + 1):\n",
    "            self.c[i] += self.c[i - 1]\n",
    "\n",
    "    def count(self, a, b):\n",
    "        if a == 0:\n",
    "            return self.c[b]\n",
    "        return self.c[b] - self.c[a - 1]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
